import datetime
import formencode

from cibynet.lib import utils

class Registration(object):
  """Resgistration object.
  """
  
  def __init__(self, check, user, room, now=None, confirmation_email=False):
    """Create a new registration request.
    The object needs to be validated before the student is activated.
    
    Arguments:
      - check: Check object, used to pay the subscription
      - user: User object, the user subscribing
      - room: Room object, the room in which the user is living.
      - now: optional, DateTime or None
    """
    now = now or datetime.datetime.now()
    self.check = check
    self.user = user
    self.room = room
    self.submit_date = now
    if not confirmation_email:
      self.confirm(now)
      self.confirmation_code = ''
    else:
      self.confirmation_code = utils.get_random_string(128)
  
  def confirm(self, now=None):
    """Mark the registration as confirmed if not already.
    
    Arguments:
      - now: optional, DateTime or None
    """
    if not self.confirmation_date:
      now = now or datetime.datetime.now()
      self.confirmation_date = now
      
  def validate(self, by_user, now=None):
    """Validate the registration: the check has been received.
    The user is activated and so:
      - the user is associated to the room 
      - the connection is activated for that room.
      - IP(s) address is/are given to the users machine(s)
    A confirmation email is sent to the user.
    """
    # TODO: finish
    now = now or datetime.datetime.now()
    self.validated_by = by_user
    self.validated_date = now
    #self.user.room = room

